﻿using CommonBehaviors.Actions;
using Portal.Core;
using Portal.Managers;
using Portal.Settings;
using JetBrains.Annotations;
using Styx;
using Styx.TreeSharp;

namespace Portal.Specialisation
{
    [UsedImplicitly]
    internal class Fire : RotationBase
    {

        #region RotationBase Overrides
        public override WoWSpec KeySpec { get { return WoWSpec.MageFire; } }
        public override string Name { get { return "Fire"; } }
        #endregion
        private static PortalSettings Settings { get { return PortalSettings.Instance; } }

        #region Honorbuddy's Rotation Overrides
        public override Composite Rotation
        {
            get
            {
                return new PrioritySelector(
                    new Decorator(ret => Me.CurrentTarget.HasDoNotDamageAura() || Me.HasAura("Ice Block"), new ActionAlwaysSucceed()),

                    Common.NonGcdCombat(),

                    new Decorator(ret => Me.IsChanneling || Me.IsCasting || Common.LoSCheck, new ActionAlwaysSucceed()),
                    
                    Common.SetCounts, // Set counts for our AoE trigger
                    Common.Combat(), // Handles all of our utility / talent abilities.


                    Spell.Cast("Presence of Mind", ret => NeedPresenceofMind),
                    Spell.PreventDoubleCast("Alter Time", 2, ret => NeedAlterTimeNoPoM || NeedAlterTimePoM),
                    Spell.Cast("Alter Time", ret => NeedForceExpireAlterTimeNoPoM || NeedForceExpireAlterTimePoM),
                    Spell.Cast("Combustion", ret => NeedCombustion),

                    // AoE selector
                    new Decorator(ret => Settings.Fire_EnableAoE && Common._nearbyAoEUnitCount >= Settings.Fire_AoEUnitCount, HandleAoECombat()),
                    HandleSingleTarget());
            }

        }


        private static Composite HandleSingleTarget()
        {
            return new PrioritySelector(
                Common.HandleSingleTargetMageBombs(),
                Spell.Cast("Pyroblast", ret => NeedPyroBlast),
                Spell.Cast("Inferno Blast", ret => NeedInfernoBlast),
                new Decorator(ret => Settings.Fire_EnableAoE, Common.HandleBombMultiDoT()),
                Spell.Cast("Fireball", ret => !Me.IsMoving()),
                Spell.CastIgnoreMoving("Scorch", on => Me.CurrentTarget, ret => Me.IsMoving())
                
                );

        }

        private static Composite HandleAoECombat()
        {
            return new PrioritySelector(
                Common.HandleBombMultiDoT(),
                Spell.Cast("Inferno Blast", ret => IgnitePyro),
                Spell.CastOnGround("Flamestrike", loc => Me.CurrentTarget.Location, ret => true),
                Spell.Cast("Dragon's Breath", ret => NeedDragonsBreath),
                Spell.Cast("Pyroblast", ret => NeedPyroBlast),
                Spell.Cast("Inferno Blast", ret => NeedInfernoBlast),
                Spell.CastIgnoreMoving("Scorch", on => Me.CurrentTarget, ret => Me.IsMoving())
                );
        }

        public override Composite PreCombat
        {
            get
            {
                return new PrioritySelector(
                    Common.PreCombat()
                    );
            }
        }

        #endregion

        #region Conditions
        private static bool IgnitePyro { get { return Me.CurrentTarget != null && Me.CurrentTarget.HasAllMyAuras("Ignite", "Pyroblast") && !Me.ActiveAuras.ContainsKey("Pyroblast!"); } }
        private static bool NeedInfernoBlast { get { return Me.HasAura("Heating Up") && !Me.HasAura("Pyroblast!"); } }

        private static bool Pyroblast { get { return Me.ActiveAuras.ContainsKey("Pyroblast!"); } }
        private static bool AlterTime { get { return Me.ActiveAuras.ContainsKey("Alter Time"); } }
        private static bool PoM { get { return Me.HasAura("Presence of Mind"); } }

        private static bool Tier6TalentAuraCheck 
        {
            get
            {
            return ((TalentManager.HasTalent(16) || TalentManager.HasTalent(17) && Me.HasAnyAura(SpellId.InvokersEnergyAura, SpellId.RuneOfPowerAura)) 
                    || TalentManager.HasTalent(18));
            }
        }

        private static bool NeedDragonsBreath { get { return (!Me.ActiveAuras.ContainsKey("Heating Up") && !Pyroblast && Me.IsSafelyFacing(Me.CurrentTarget) && Me.CurrentTarget.Distance <= 8); } }
        private static bool NeedAlterTimeNoPoM { get
        {
            return Settings.UseAlterTimeOffensive && !TalentManager.HasTalent(1) && Tier6TalentAuraCheck && Pyroblast && !AlterTime;
        } }
        private static bool NeedAlterTimePoM { get
        {
            return Settings.UseAlterTimeOffensive && TalentManager.HasTalent(1) && Tier6TalentAuraCheck && Pyroblast && PoM && !AlterTime;
        } }
        private static bool NeedForceExpireAlterTimePoM { get { return Settings.UseAlterTimeOffensive && TalentManager.HasTalent(1) && !Pyroblast && !PoM && AlterTime; } }
        private static bool NeedForceExpireAlterTimeNoPoM { get { return Settings.UseAlterTimeOffensive && !TalentManager.HasTalent(1) && !Pyroblast && AlterTime; } }
        private static bool NeedPresenceofMind
        {
            get
            {
                return Settings.UsePresenceOfMind && Settings.PresenceOfMindCondition == 4 && TalentManager.HasTalent(1) &&
                    Pyroblast && Tier6TalentAuraCheck && !Spell.SpellOnCooldown("Alter Time");
            }
        }
        private static bool NeedPyroBlast { get { return PoM || Pyroblast; } }

        private static bool NeedCombustion
        {
            get
            {
                var igniteRemains = Spell.GetMyAuraTimeLeft("Ignite", Me.CurrentTarget) > 0;

                if (!igniteRemains)
                {
                    return false;
                }

                if (Settings.Fire_CombustionCooldown && Spell.GetMyAuraTimeLeft("Pyroblast", Me.CurrentTarget) > 0)
                {
                    return true;
                }

                return Settings.Fire_CombustionPoMAlterTimeCombo && !Pyroblast && Spell.Lastspellcast == "Pyroblast" && !Me.HasAnyAura("Alter Time", "Presence of Mind");
            }
        }
        #endregion

    }
}
